package zhihui.mobile.ui.register;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zgm.utils.AppUtils;
import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.LoadingDialog;

import zhihui.mobile.R;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.register.AddressFragmentPresenter;
import zhihui.mobile.presenter.register.IAddressFragmentContract;
import zhihui.mobile.presenter.register.RegisterPersenter;
import zhihui.mobile.ui.BaseFragment;
import zhihui.mobile.ui.address.AddressActivity;

/**
 * Created by 张公铭 on 2017/7/5 0005.
 */

public class AddressFragment extends BaseFragment implements IAddressFragmentContract.IAddressFragmentView
{
    private AddressFragmentPresenter addressPresenter = new AddressFragmentPresenter();

    private RelativeLayout rl_address;
    private TextView tv_address;
    private Button btn_next;
    private RegisterPersenter registerPersenter;
    private CheckBox ck_agree;
    private TextView tv_msg;
    private TextView tv_xieyi;
    private boolean isLoaction = true;  //是否发起定位

    public void setLoaction(boolean loaction)
    {
        isLoaction = loaction;
    }

    public void setRegisterPersenter(RegisterPersenter registerPersenter)
    {
        this.registerPersenter = registerPersenter;
    }

    @Override
    public void initView()
    {
        rl_address = (RelativeLayout) mView.findViewById(R.id.rl_register_address);
        rl_address.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(getActivity(), AddressActivity.class);
                getActivity().startActivity(intent);
            }
        });
        tv_address = (TextView) mView.findViewById(R.id.tv_register_address);

        btn_next = (Button) mView.findViewById(R.id.btn_register_address_next);
        btn_next.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (TextUtils.isEmpty(tv_address.getText()))
                {
                    showMessage("请选择小区！");
                    return;
                }
                if (!ck_agree.isChecked())
                {
                    showMessage("请同意用户协议！");
                    return;
                }
                tv_msg.setText(" ");
                registerPersenter.showPhoneFragment();
            }
        });


        ck_agree = (CheckBox) mView.findViewById(R.id.cb_register_deal);

        tv_msg = (TextView) mView.findViewById(R.id.tv_register_address_msg);
        tv_msg.setText(" ");
        tv_xieyi = (TextView) mView.findViewById(R.id.tv_register_xieyi);
        tv_xieyi.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.register_page_fragment;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{addressPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        addressPresenter.init(this, getActivity());
    }

    @Override
    public void showLoading()
    {
        if (mLoadingDialog == null)
        {
            mLoadingDialog = new LoadingDialog(getActivity(), R.drawable.loading_image);
        }
        mLoadingDialog.setText("获取定位信息...");
        mLoadingDialog.dismiss();
        mLoadingDialog.show();
    }

    @Override
    public void showLocationDialog()
    {
        UiUtils.showDialog(getContext(), "提示", "定位服务关闭，是否开启？", "设置", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                AppUtils.openGps(getContext());
                dialog.dismiss();
                dialog.cancel();
                dialog = null;
            }
        }, "取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                dialog.cancel();
                dialog = null;
            }
        });
    }

    @Override
    public void showDialog(String message)
    {
        UiUtils.showDialog(getContext(), null, message);
    }

    @Override
    public void initCommunityEdit(String name)
    {
        tv_address.setText(name);
    }

    @Override
    public boolean isLocation()
    {
        return isLoaction;
    }

    @Override
    public void showMessage(String message)
    {
        tv_msg.setText(message);
    }
}
